<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="用户" prop="attorneyName.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择用户"
            labelName = 'userName'
            labelValue = 'id'
            :value = "inputForm.attorneyName.id"
            :limit="1"
            @getValue='(value) => {inputForm.attorneyName.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'userName',
              label: '用户名'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'userName',
              label: '用户名'
            }
            ]"
            dataListUrl="/dlyrl/hr/userinformation/userInformation/list"
            entityBeanName="userInformation"
            queryEntityUrl="/dlyrl/hr/userinformation/userInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="地点" prop="address"
                :rules="[
                 ]">
              <el-input v-model="inputForm.address" placeholder="请填写地点"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="业务领域" prop="businessArea"
                :rules="[
                 ]">
              <el-input v-model="inputForm.businessArea" placeholder="请填写业务领域"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="是否收费" prop="isCharge"
                :rules="[
                 ]">
                <el-select v-model="inputForm.isCharge" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('yes_no')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="价格" prop="price"
                :rules="[
                 ]">
              <el-input v-model="inputForm.price" placeholder="请填写价格"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="分类" prop="classify"
                :rules="[
                 ]">
              <el-input v-model="inputForm.classify" placeholder="请填写分类"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="个人标签" prop="personalLabel"
                :rules="[
                 ]">
              <el-input v-model="inputForm.personalLabel" placeholder="请填写个人标签"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="区域" prop="area"
                :rules="[
                 ]">
              <el-input v-model="inputForm.area" placeholder="请填写区域"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="头像" prop="avatar"
                :rules="[
                 ]">
              <el-upload ref="avatar"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl/hr/attorney/attorney`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file) => {$window.open ((file.response && file.response.url) || file.url,'_blank') }"
                    :on-success="(response, file, fileList) => {
                       inputForm.avatar = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.avatar = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="avatarArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="执业证" prop="license"
                :rules="[
                 ]">
              <el-upload ref="license"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl/hr/attorney/attorney`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file) => {$window.open ((file.response && file.response.url) || file.url,'_blank') }"
                    :on-success="(response, file, fileList) => {
                       inputForm.license = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.license = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="licenseArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="律师简介" prop="attorneyProfile"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.attorneyProfile" placeholder="请填写律师简介"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="成功案例" prop="successCase"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.successCase" placeholder="请填写成功案例"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="擅长领域" prop="goodField"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.goodField" placeholder="请填写擅长领域"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="证书编号" prop="certificateNumber"
                :rules="[
                 ]">
              <el-input v-model="inputForm.certificateNumber" placeholder="请填写证书编号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        avatarArra: [],
        licenseArra: [],
        inputForm: {
          id: '',
          attorneyName: {
            id: ''
          },
          address: '',
          businessArea: '',
          isCharge: '',
          price: '',
          remarks: '',
          classify: '',
          personalLabel: '',
          avatar: '',
          license: '',
          attorneyProfile: '',
          successCase: '',
          goodField: '',
          area: '',
          certificateNumber: ''
        }
      }
    },
    components: {
      GridSelect
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建律师`
        } else if (method === 'edit') {
          this.title = '修改律师'
        } else if (method === 'view') {
          this.title = '查看律师'
        }
        this.avatarArra = []
        this.licenseArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/attorney/attorney/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.attorney)
              this.inputForm.avatar.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.avatarArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.license.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.licenseArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/attorney/attorney/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>